package com.questionnaires.service;

import com.questionnaires.model.Question;
import com.questionnaires.model.Questionnaire;
import com.questionnaires.model.Response;
import com.questionnaires.model.UserResponse;
import com.questionnaires.repository.QuestionRepository;
import com.questionnaires.repository.QuestionnaireRepository;
import com.questionnaires.repository.ResponseRepository;
import com.questionnaires.repository.UserResponseRepository;
import com.questionnaires.utils.ResponsePercentage;
import com.questionnaires.utils.ResponseQuestionStatistics;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author adriana.calina.oros@gmail.com
 */

@Service(value = "userResponseService")
public class UserResponseServiceImpl implements UserResponseService {

    @Autowired
    private UserResponseRepository userResponseRepository;

    @Autowired
    private QuestionRepository questionRepository;

    @Autowired
    private ResponseRepository responseRepository;

    @Autowired
    private QuestionnaireRepository questionnaireRepository;


    public void saveUserResponses(List<Response> responses) {
        for (Response response : responses) {
            UserResponse userResponse = userResponseRepository.findByResponse(response);
            if (userResponse == null) {
                userResponse = new UserResponse();
                userResponse.setResponse(response);
                userResponse.setResponseCount(1);
                userResponseRepository.insert(userResponse);
            } else {
                userResponse.setResponseCount(userResponse.getResponseCount() + 1);
                userResponseRepository.update(userResponse);
            }
        }
    }

    public List<ResponseQuestionStatistics> getStatistics(long questionnaireId) {
        List<ResponseQuestionStatistics> questionStatisticsList = new ArrayList<ResponseQuestionStatistics>();

        //get the questionnaire populated with all fields
        Questionnaire questionnaire = questionnaireRepository.findById(questionnaireId);
        List<Question> questionList = questionnaire.getQuestions();

        for (Question question : questionList) {
            long responseTotalNumber = 0; //total number of responses per question
            List<ResponsePercentage> responsePercentageList = new ArrayList<ResponsePercentage>();

            //get responses of a question
            Question questionFilled = questionRepository.findById(question.getId());
            List<Response> responseList = questionFilled.getResponses();
            for (Response response : responseList) {
                UserResponse userResponse = userResponseRepository.findByResponse(response);
                long responseNumber = 0;
                if (userResponse != null) {
                    responseNumber = userResponse.getResponseCount();
                }
                ResponsePercentage responsePercentage = new ResponsePercentage();
                responsePercentage.setResponse(response);
                responsePercentage.setPercentage(responseNumber);
                responsePercentageList.add(responsePercentage);
                responseTotalNumber += responseNumber;
            }

            for (ResponsePercentage responsePercentage : responsePercentageList) {
                double percent = responsePercentage.getPercentage() * 100 / responseTotalNumber;
                responsePercentage.setPercentage(percent);
            }

            ResponseQuestionStatistics responseQuestionStatistics = new ResponseQuestionStatistics();
            responseQuestionStatistics.setQuestion(questionFilled);
            responseQuestionStatistics.setResponsePercentage(responsePercentageList);
            questionStatisticsList.add(responseQuestionStatistics);
        }

        return questionStatisticsList;
    }

}
